package com.hsnn.medstgmini.trading.pur.controller;

import java.io.BufferedOutputStream;
import java.io.OutputStream;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hsnn.medstgmini.Constants;
import com.hsnn.medstgmini.base.dict.model.form.DicAreaForm;
import com.hsnn.medstgmini.base.dict.service.DicAreaManager;
import com.hsnn.medstgmini.base.std.model.StdCompinfo;
import com.hsnn.medstgmini.exception.MSException;
import com.hsnn.medstgmini.sys.model.SysUser;
import com.hsnn.medstgmini.trading.pur.model.OrderPackStatus;
import com.hsnn.medstgmini.trading.pur.model.form.OrderPack;
import com.hsnn.medstgmini.trading.pur.model.form.OrderPackage;
import com.hsnn.medstgmini.trading.pur.service.OrderConfirmManager;
import com.hsnn.medstgmini.trading.pur.service.OrderDispatchManager;
import com.hsnn.medstgmini.util.DbUtil;
import com.hsnn.medstgmini.util.ExcelUtil;
import com.hsnn.medstgmini.util.Pagination;

/**
 * @category 订单配送
 * @author 蔡春龙
 * @date 2015年7月14日
 */
@Controller
@RequestMapping("/orderDispatch")
public class OrderDispatchController {

	private static final Logger log = Logger
			.getLogger(OrderDispatchController.class);

	@Autowired
	private OrderConfirmManager orderConfirmManager;

	@Autowired
	private OrderDispatchManager orderDispatchManager;

	@Autowired
	private DicAreaManager dicAreaManager;

	@Value("#{sysProp['system.province']}")
	private String province;

	@Autowired
	private HttpServletRequest request;

	@Autowired
	private HttpSession session;

	/**
	 * @category 获取当前登陆人信息
	 * @date 2015年6月14日
	 * @return
	 */
	public SysUser getUser() {
		return (SysUser) session.getAttribute(Constants.USERINFO);
	}

	/**
	 * @category 进入订单包列表页面
	 * @date 2015年7月13日
	 * @return
	 */
	@RequestMapping("/toOrderDispatch")
	public String toOrderDispatch() {
		DicAreaForm area = dicAreaManager.getDicAreaById(province);
		request.setAttribute("area", area);
		return "/trading/pur/orderDispatch";
	}

	/**
	 * @category 查询订单包列表
	 * @date 2015年7月13日
	 * @return
	 */
	@RequestMapping("/getOrderDispatch")
	@ResponseBody
	public Pagination getOrderDispatch(OrderPackage orderPackage) {
		log.info("[配送企业]查询订单包列表："+orderPackage);
		Pagination page = new Pagination(request);
		try {
			orderPackage.setOrderStatus(OrderPackStatus.confirmed.getKey());
			StdCompinfo comp = getUser().getInfo();
			orderPackage.setDelCompCode(comp.getCompCode());
			orderDispatchManager.getOrderDispatch(orderPackage, page);
			page.setSuccess(true);
		} catch (Exception e) {
			log.error("",e);
		}
		return page;
	}

	/**
	 * @category 进入订单详细列表页面
	 * @date 2015年7月13日
	 * @return
	 */
	@RequestMapping("/toOrderDispatchDetail")
	public String toOrderDispatchDetail(Model model, String orderPackId) {
		log.info("[配送企业]进入订单详细列表页面ID："+orderPackId);
		Map<String, Object> map = orderConfirmManager
				.getOrderPackageById(orderPackId);
		model.addAttribute("orderPackId", orderPackId);
		model.addAttribute("orderPack", map);
		return "/trading/pur/orderDispatchDetail";
	}

	/**
	 * @category 查询订单详细列表
	 * @date 2015年7月13日
	 * @param orderPackage
	 * @return
	 */
	@RequestMapping("/getOrderDispatchDetail")
	@ResponseBody
	public Pagination getOrderDispatchDetail(OrderPackage orderPackage) {
		log.info("[配送企业]查询订单详细列表："+orderPackage);
		Pagination page = new Pagination(request);
		try {
			orderPackage.setOrderStatus(OrderPackStatus.confirmed.getKey());
			StdCompinfo comp = getUser().getInfo();
			orderPackage.setDelCompId(comp.getCompId());
			orderConfirmManager.getOrderConfirmDetail(orderPackage, page);
			page.setSuccess(true);
		} catch (Exception e) {
			log.error("",e);
		}
		return page;
	}

	/**
	 * @category 配送订单
	 * @date 2015年11月26日
	 * @param array
	 * @return
	 */
	@RequestMapping("/dispatchDetail")
	@ResponseBody
	public Pagination dispatchDetail(OrderPack orderPack) {
		log.info("[配送企业]配送订单："+orderPack);
		Pagination page = new Pagination(request);
		try {
			orderDispatchManager.dispatchDetail(orderPack, getUser());
			page.setSuccess(true);
		} catch (MSException e) {
			log.error("",e);
			page.setMsg(e.getMsg());
		} catch (Exception e) {
			log.error("",e);
		}
		return page;
	}
	/**
	 * 
	 *@category 缺货
	 *@author 邱磊
	 *@date 2015年12月13日 下午2:06:47
	 *@param 
	 *@return Pagination
	 */
	@RequestMapping("/shortDrug")
	@ResponseBody
	public Pagination shortDrug(OrderPack orderPack){

		log.info("[配送企业]订单缺货："+orderPack);
		Pagination page = new Pagination(request);
		try {
			orderDispatchManager.shortDrug(orderPack, getUser());
			page.setSuccess(true);
		} catch (MSException e) {
			log.error("",e);
			page.setMsg(e.getMsg());
		} catch (Exception e) {
			log.error("",e);
		}
		return page;
	
	}
	
	/**
	 * @category 拒绝配送
	 * @date 2015年11月26日
	 * @param array
	 * @return
	 */
	@RequestMapping("/refuseDispatchDetail")
	@ResponseBody
	public Pagination refuseDispatchDetail(OrderPack orderPack) {
		log.info("enter refuseDispatchDetail method");
		Pagination page = new Pagination(request);
		try {
			orderDispatchManager.refuseDispatchDetail(orderPack, getUser());
			page.setSuccess(true);
		} catch (MSException e) {
			log.error("",e);
			page.setMsg(e.getMsg());
		} catch (Exception e) {
			log.error("",e);
		}
		return page;
	}
	
	
	
	/**
	 * @category 导出配送订单明细
	 * @date 2015年7月13日
	 * @param orderPackage
	 * @return
	 */
	@RequestMapping("/exportExcelByOrderDispatchDetail")
	@ResponseBody
	public Pagination exportExcelByOrderDispatchDetail(HttpServletResponse response) {
		
		Pagination page = new Pagination(request);
		DbUtil.jsonToMap(request, page);
		
		try {
			String title = "订单包号："+request.getParameter("orderPackId")+"明细";
			page.getConditions().put("NoPage", "1");
			OrderPackage orderPackage = new OrderPackage();
			orderPackage.setOrderStatus(OrderPackStatus.confirmed.getKey());
			StdCompinfo comp = getUser().getInfo();
			orderPackage.setDelCompId(comp.getCompId());
			orderPackage.setDrugCode(request.getParameter("drugCode"));
			orderPackage.setDrugName(request.getParameter("drugName"));
			orderPackage.setCompName(request.getParameter("compName"));
			orderPackage.setOrderPackId(request.getParameter("orderPackId"));
			
			title = ExcelUtil.processFileName(request, title);  
			response.addHeader("Content-Disposition", "attachment;filename="+title+".xls");
			OutputStream os= new BufferedOutputStream(response.getOutputStream());  
			response.setContentType("application/vnd.ms-excel;charset=utf-8");  
			
			page = orderConfirmManager.getOrderConfirmDetail(orderPackage, page);
			String[] headers = {"采购价","采购数量","采购金额","通用名","剂型","规格","转换系数","单位","生产企业", "状态"};
			String[] alias = {"proPrice","proCount","orderMoney","drugName","formName","specName","factor","unitName", "prodCompname", "orderStatus"};
			ExcelUtil.exportExcel(page, os, "配送订单明细", headers, alias);
			page.setSuccess(true);
		} catch (Exception e) {
			page.setSuccess(false);
			e.printStackTrace();
		}
		return page;
	}
}
